package com.agileboot.attendance.domain.settlement.service;

import com.agileboot.attendance.domain.settlement.dto.SettlementApproveDTO;
import com.agileboot.attendance.domain.settlement.entity.AttSettlementEntity;
import com.agileboot.attendance.domain.settlement.enums.SettlementErrorCode;
import com.agileboot.attendance.domain.settlement.enums.SettlementStatus;
import com.agileboot.attendance.domain.settlement.mapper.AttSettlementMapper;
import com.agileboot.common.exception.ApiException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 结算单审核服务
 */
@Service
@RequiredArgsConstructor
public class SettlementApproveService extends ServiceImpl<AttSettlementMapper, AttSettlementEntity> {

    /**
     * 提交审核
     */
    @Transactional(rollbackFor = Exception.class)
    public void submit(Long id) {
        AttSettlementEntity entity = getById(id);
        if (entity == null) {
            throw new ApiException(SettlementErrorCode.SETTLEMENT_NOT_FOUND);
        }

        if (entity.getStatus() != SettlementStatus.DRAFT) {
            throw new ApiException(SettlementErrorCode.SETTLEMENT_DRAFT_ONLY);
        }

        entity.setStatus(SettlementStatus.PENDING_PARTY_B_REVIEW);
        updateById(entity);
    }

    /**
     * 审核结算单
     */
    @Transactional(rollbackFor = Exception.class)
    public void approve(SettlementApproveDTO approveDTO) {
        AttSettlementEntity entity = getById(approveDTO.getId());
        if (entity == null) {
            throw new ApiException(SettlementErrorCode.SETTLEMENT_NOT_FOUND);
        }

        // 校验状态
        checkStatus(entity, approveDTO.getApproveType());

        // 更新审核信息
        if (approveDTO.getApproved()) {
            // 审核通过
            if (approveDTO.getApproveType() == 1) {
                // 乙方审核通过，进入甲方审核
                entity.setStatus(SettlementStatus.PENDING_PARTY_A_REVIEW);
                entity.setPartyBApproveId(getCurrentUserId());
                entity.setPartyBApproveBy(getCurrentUsername());
                entity.setPartyBApproveTime(LocalDateTime.now());
                entity.setPartyBApproveRemark(approveDTO.getApproveRemark());
            } else {
                // 甲方审核通过，结算单完成
                entity.setStatus(SettlementStatus.PARTY_A_APPROVED);
                entity.setPartyAApproveId(getCurrentUserId());
                entity.setPartyAApproveBy(getCurrentUsername());
                entity.setPartyAApproveTime(LocalDateTime.now());
                entity.setPartyAApproveRemark(approveDTO.getApproveRemark());
                // TODO: 更新关联的验收单状态为已结算
            }
        } else {
            // 审核不通过
            if (approveDTO.getApproveType() == 1) {
                // 乙方审核不通过
                entity.setStatus(SettlementStatus.PARTY_B_REJECTED);
                entity.setPartyBApproveId(getCurrentUserId());
                entity.setPartyBApproveBy(getCurrentUsername());
                entity.setPartyBApproveTime(LocalDateTime.now());
                entity.setPartyBApproveRemark(approveDTO.getApproveRemark());
            } else {
                // 甲方审核不通过
                entity.setStatus(SettlementStatus.PARTY_A_REJECTED);
                entity.setPartyAApproveId(getCurrentUserId());
                entity.setPartyAApproveBy(getCurrentUsername());
                entity.setPartyAApproveTime(LocalDateTime.now());
                entity.setPartyAApproveRemark(approveDTO.getApproveRemark());
            }
        }

        updateById(entity);
    }

    /**
     * 校验结算单状态
     */
    private void checkStatus(AttSettlementEntity entity, Integer approveType) {
        if (approveType == 1) {
            // 乙方审核
            if (entity.getStatus() != SettlementStatus.PENDING_PARTY_B_REVIEW) {
                throw new ApiException(SettlementErrorCode.SETTLEMENT_STATUS_ERROR);
            }
        } else if (approveType == 2) {
            // 甲方审核
            if (entity.getStatus() != SettlementStatus.PENDING_PARTY_A_REVIEW) {
                throw new ApiException(SettlementErrorCode.SETTLEMENT_STATUS_ERROR);
            }
        } else {
            throw new ApiException(SettlementErrorCode.APPROVE_TYPE_ERROR);
        }
    }

    private Long getCurrentUserId() {
        // TODO: 从当前登录用户中获取用户ID
        return 1L;
    }

    private String getCurrentUsername() {
        // TODO: 从当前登录用户中获取用户名
        return "admin";
    }
}
